package com.dorm.cj.completablefuture;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

public class CompletableFutureTest {

    public static void main(String[] args) throws IOException {
//        CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread());
//        }).thenAccept((result) -> {
//            System.out.println(result);
//        }).exceptionally((e) -> {
//            e.printStackTrace();
//            return null;
//        });
//
//        CompletableFuture.supplyAsync(CompletableFutureTest::test)
//                .thenAccept((result) -> {
//                    System.out.println(result);
//                }).exceptionally((e) -> {
//            e.printStackTrace();
//            return null;
//        });
//
//        CompletableFuture.supplyAsync(CompletableFutureTest::test2)
//                .thenAccept((result) -> {
//                    System.out.println(result);
//                }).exceptionally((e) -> {
//            e.printStackTrace();
//            return null;
//        });

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(CompletableFutureTest::test);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(CompletableFutureTest::test);
        CompletableFuture<Object> future = CompletableFuture.anyOf(future1, future2);
        future.thenAccept((res)->{
            System.out.println(res);
        });
        System.out.println("-----------------分割线-----------------");
        future1.thenCombineAsync(future2,(f1,f2)->{return f1+f2;}).thenAccept((res)->{
            System.out.println(res);
        });

        System.in.read();
    }

    static String test() {
        return "hhhh";
    }

    static String test2() {
        throw new RuntimeException("kkkk");
    }
}
